﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dixon.Jscp
{
    
    /// <summary>
    /// Represents the configuration of the JavaScript Client Pages 
    /// environment
    /// </summary>
    public abstract class Configuration
    {
        /// <summary>
        /// The path to source JavaScript Client pages
        /// containing code and markup
        /// </summary>
        public abstract string SourcePagePath
        {
            get;
        }

        /// <summary>
        /// The path to compiled JavaScript Client Pages
        /// implemented in "DOM Assembly" (DOM-generating JavaScript code
        /// running within the context of DiXon)
        /// </summary>
        public abstract string CompiledPagePath
        {
            get;
        }

        /// <summary>
        /// The path to the XSLT stylesheet of the DiXon compiler
        /// </summary>
        public abstract string CompilerStylesheetPath
        {
            get; 
        }


        /// <summary>
        /// The Internet content type (a.k.a. MIME type) used to serve scripts.
        /// For JavaScript, the most commonly recognized MIME types are 
        /// <list type="unordered">
        ///     <item>application/x-javascript</item>
        ///     <item>application/x-ecmascript</item>
        ///     <item>text/javascript</item>
        ///     <item>text/ecmascript</item>
        /// </list>
        /// </summary>
        /// <remarks>
        ///     <para>
        ///         Most browsers will correctly interpret scripts referenced from a 
        ///         Web page even if the content type is completely bogus because the
        ///         type specified on the Web page overrides the HTTP header
        ///     </para>
        ///     <para>
        ///         To see the contents of the script in Internet Explorer, set this
        ///         to text/plain
        ///     </para>
        /// </remarks>
        public abstract string ScriptContentType
        {
            get;
        }

        /// <summary>
        /// Computes the path to the source code of the specified 
        /// JavaScript client page
        /// </summary>
        /// <param name="pageFileName">
        ///     the relative path to the page
        /// </param>
        /// <returns>the absolute path</returns>
        public string GetSourcePath(string pageFileName)
        {
            return SourcePagePath + Path.DirectorySeparatorChar + pageFileName;
        }

        /// <summary>
        /// Computes the path to the compiled JavaScript / DiXon DOM Assembly
        /// code for the specified page
        /// </summary>
        /// <param name="pageFileName">
        ///     a relative path to the page
        /// </param>
        /// <returns></returns>
        public string GetCompiledPath(string pageFileName)
        {
            return CompiledPagePath + Path.DirectorySeparatorChar + pageFileName;
        }

        /// <summary>
        /// Creates a directory for a projected file path, relative to a root that
        /// exists "for sure".
        /// </summary>
        /// <param name="root"></param>
        /// <param name="filePath"></param>
        public static void MakeDirectory(string root, string filePath)
        {
            if (!Directory.Exists(root))
            {
                throw new RequiredDirectoryNotFoundException(root);
            }
            string[] directories = filePath.Split('/');
            int last = directories.Length - 1;

            string fullName = root;
            for (int i = 0; i < last; i++)
            {
                fullName += Path.DirectorySeparatorChar + directories[i];
                if (!Directory.Exists(fullName))
                {
                    Directory.CreateDirectory(fullName);
                }
            }
        }

    }
}
